<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <iframe id="iframe" style="width: 500px;height: 500px;"
        src="https://api-dev.zmjk.trtjk.com/zhima-product-center-api/doc.html#/default/%E8%B4%AD%E7%89%A9%E8%BD%A6API/queryCartUsingPOST"
        frameborder="0"></iframe>
</body>
<script>
    document.getElementById('iframe').onload = function () {
        console.log('iframe加载完成');
        const iframe = document.getElementById('iframe');
        iframe = iframe.contentDocument || iframe.contentWindow.document
        console.dir(iframe)
        let requset,
            minLevel = 100,
            maxLevel = 0,
            objectArray = [],
            load = { datas: [] },
            continuity = false,
            textLoad = "";
        console.log(iframe.ownerDocument.documentElement.getElementsByClassName("api-title"));
        iframe.ownerDocument.getElementsByClassName("api-title").forEach((item) => {
            if (item.innerText === "响应参数")
                requset = item.nextSibling.getElementsByClassName("ant-table-row");
        });
        // 找到最大level
        requset.forEach((item) => {
            if (item.className.split("").at(-1) > maxLevel)
                maxLevel = item.className.split("").at(-1);
            if (item.className.split("").at(-1) < minLevel)
                minLevel = item.className.split("").at(-1);
            objectArray.push({
                class: item.className.split("").at(-1),
                key: item.children[0].innerText,
                title: item.children[1].innerText,
                type: item.children[2].innerText,
            });
        });
        while (minLevel <= maxLevel) {
            for (let i = objectArray.length - 1; i >= 0; i--) {
                const item = objectArray[i];
                if (item.class != maxLevel) continuity = false;
                if (item.class == maxLevel) {
                    if (load.datas.length == 0) load.end = i;
                    continuity = true;
                    load.datas.push(item);
                }
                if (load?.datas.length > 0 && !continuity) {
                    load.start = i + 1;
                    item.children = load.datas;
                    generate(item, load);
                    objectArray.splice(load.start, load.end - load.start + 1);
                    load = { datas: [] };
                }
            }
            maxLevel--;
        }
        // console.log(objectArray);
        console.log(textLoad);
        function generate(item, load) {
            let temp = `class LL${capitalizeFirstLetter(
                item.key
            )}Model: LLBaseModel { \n`;
            let mapping = "\n\toverride func mapping(map: Map) { \n";
            load.datas.forEach((value) => {
                let type = value.type;
                let tempType = "";

                if (type.indexOf("integer") >= 0) tempType = "Int = 0";
                else if (type.indexOf("string") >= 0) tempType = 'String = ""';
                else if (type.indexOf("boolean") >= 0) tempType = "Bool = false";
                else if (type.indexOf("array") >= 0)
                    tempType = `[LL${capitalizeFirstLetter(value.key)}Model] = []`;
                temp += `\n\t///${value.title}\n`;
                temp += `\tvar ${value.key}: ${tempType}\n`;
                mapping += `\t\t${value.key} <- map["${value.key}"]\n`;
            });
            mapping += "\t}\n";
            temp += mapping;
            temp += "}\n";
            textLoad = temp + textLoad;
        }

        function capitalizeFirstLetter(string) {
            return string.charAt(0).toUpperCase() + string.slice(1);
        }

    }
</script>

</html>